var nowDate = getDay(0);
var pageType = sessionStorage.getItem('pageType');
if(pageType == '' && sessionStorage.getItem("pageType") != 'null' && sessionStorage.getItem("pageType") != null){
	pageType = sessionStorage.getItem("pageType");
}
var corp = GetQueryString('corp')==null?"":GetQueryString('corp');
if(corp==''){
	corp = sessionStorage.getItem("corp");
}else{
	sessionStorage.setItem("corp",corp);
}
var shopcode = GetQueryString('shopcode')==null?"":GetQueryString('shopcode');
var startDate = GetQueryString('startDate')==null?nowDate:GetQueryString('startDate');
var endDate = GetQueryString('endDate')==null?nowDate:GetQueryString('endDate');
var calendar1 = new lCalendar();
var calendar2 = new lCalendar();
var url;
calendar1.init({
	'trigger': '#startDate',
	'type': 'date',
	'minDate': '2022-01-01',
	'maxDate': nowDate
});
calendar2.init({
	'trigger': '#endDate',
	'type': 'date',
	'minDate': '2022-01-01',
	'maxDate': nowDate
});
function ICalendarClick(){
	if($('#startDate').val() <= $('#endDate').val()){
		startDate = $('#startDate').val();
		endDate = $('#endDate').val();
	}else{
		$('#startDate').val(startDate);
		$('#endDate').val(endDate);
		dd.ready(function(){
			dd.device.notification.alert({
				message: '开始日期不能大于结束日期',
				title: "提示",//可传空
				buttonName: "确定",
				onSuccess : function() {
					
				},
				onFail : function(err) {}
			});
		});
	}
}
//sessionStorage.setItem("userId","193114475332300122"); 
//sessionStorage.setItem("name","任丽琴");
//sessionStorage.setItem("jobnumber","009028");
//sessionStorage.setItem("deptId","55088496");
//sessionStorage.setItem("mobile","18334724145");
if(sessionStorage.getItem("name")){
	//alert(localStorage.getItem("name"));
//	setthenum();
//	if(pageType == 'yingyun'){
//		getYingyunUser();
//	}
	onload();
}else{

	var _config ;
	$.ajax({
		type : "post",
		data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26'),"corp":corp}),
		contentType: "application/json; charset=utf-8",
		async: false,
		url : "../../dingding/getconfig",
		dataType : "json",
		success : function(suc) {
			_config = suc.config;
		}
	}); 
//	dd.config({
//		agentId : _config.agentid,
//		corpId : _config.corpId,
//		timeStamp : _config.timeStamp,
//		nonceStr : _config.nonceStr,
//		signature : _config.signature,
//		jsApiList : [ 'runtime.info', 'biz.contact.choose',
//				'device.notification.confirm', 'device.notification.alert',
//				'device.notification.prompt', 'biz.ding.post',
//				'biz.util.openLink' ]
//	});
	dd.ready(function() {
		//channel runtime  requestOperateAuthCode requestAuthCode
	    dd.runtime.permission.requestAuthCode({
	        corpId: _config.corpId,
	        onSuccess: function(result) {
	        	getuserId(result);
	        },
	        onFail : function(err) {
	        	alert("错误"+JSON.stringify(err))
	        }
	 
	    });
	});
	function getuserId(result){
			    //$("body").text('https://oapi.dingtalk.com/user/getuserinfo?code=' + result.code + '&access_token='+ _config.token);
		$.ajax({
			url : '../../dingding/ddEasy',
			type : 'POST',
			data:JSON.stringify({"code":result.code,"token":_config.token}),
			contentType: "application/json; charset=utf-8",
			dataType : "json",
			success : function(suc) {
			    if(suc.status == '1002'){
			    	sessionStorage.setItem("userId",suc.userJson.userid);
			    	sessionStorage.setItem("name",suc.userJson.name);
			    	sessionStorage.setItem("jobnumber",suc.userJson.jobnumber);
			    	sessionStorage.setItem("deptId",suc.userJson.department);
			    	sessionStorage.setItem("mobile",suc.userJson.mobile);
//					setthenum();
//					if(pageType == 'yingyun'){
//						getYingyunUser();
//					}
					onload();
			    	
			    }else{
			    	layer.alert(suc.msg);
			    }

			},
			error : function(xhr, errorType, error) {
					alert(errorType + ', ' + error);
				}
		});
	}
	dd.error(function(err) {
		alert('dd error: ' + JSON.stringify(err));
	});
}
function onload(){
	$('#name').text(sessionStorage.getItem("name"));
	$('#startDate').val(startDate);
	$('#endDate').val(endDate);
	if(shopcode != ''){
		$('#shopcode').val(shopcode);
		$('#shopcode').attr('readonly','readonly');
	}
	$('#searchButton').on('click',function(){
		getJianchaJieguo();
	});
//	if(sessionStorage.getItem("job") == 'Store'){
//		$('#pandianShop').val(sessionStorage.getItem("shopcode"))
//		$('#pandianShop').attr('readonly','readonly');
//	}
	getJianchaJieguo();
};

//查询门店信息
function getJianchaJieguo(){
	showloading();
	var sendData = {};
	sendData.shopcode=$('#shopcode').val();
	sendData.startDate=startDate;
	sendData.endDate=endDate;
	sendData.notflag='0';
//	if(pageType == 'yingyun'){
//		sendData.jobnumber=sessionStorage.getItem("jobnumber");
//		sendData.job=sessionStorage.getItem("job");
//	}
//	if(pageType == 'pandian'){
//		sendData.wanchengUser=sessionStorage.getItem("userId");
//	}
	$.ajax({
		url: "getHuodong_jiancha_jd_every"
		,type:'post'
//		,async: false
		,data:JSON.stringify(sendData)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			console.log(res)
			$('#opeTable').empty();
			try {
				if(res.success){
					var resdata = res.data;
					for (var i = 0; i < resdata.length; i++) {
						resdata[i].shopname = resdata[i].shopname.replace('今度','').replace('烘焙','');
						$('#opeTable').append('<tr class="thin-border-bottom" onclick="jianchaItem('+resdata[i].id+',\''+resdata[i].shopcode+'\',\''+resdata[i].shopname+'\')">'
								+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].shopcode+'</td>'
								+'	<td class="orange" style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].shopname+'</td>'//id="'+el+obj+i+'"
								+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].jianchaDate+'</td>'//id="'+el+obj+i+'"
								+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].maxScore+'</td>'//id="'+el+obj+i+'"
								+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].score+'</td>'//id="'+el+obj+i+'"
								+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">'+resdata[i].lrrname+'</td>'//id="'+el+obj+i+'"
								+'</tr>');
					}
					
				}else{
					$('#opeTable').append('<tr class="thin-border-bottom">'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">--</td>'
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">--</td>'//id="'+el+obj+i+'"
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">--</td>'//id="'+el+obj+i+'"
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">--</td>'//id="'+el+obj+i+'"
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">--</td>'//id="'+el+obj+i+'"
							+'	<td style="padding-left: 0px;padding-right: 0px;display: table-cell; vertical-align: middle;">--</td>'//id="'+el+obj+i+'"
							+'</tr>');
				}
			} catch (e) {
				console.log(e);
			} finally{
				hideloading();
			}
			
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
			 hideloading();
		}
	});
	
}

//检查详情
function jianchaItem(everyId,shopcode,shopname){
	sessionStorage.setItem("shopcode",shopcode);
	sessionStorage.setItem("shopname",shopname);
	window.location.href='toJiancha_jieguo_item?everyId='+everyId;
}